package com.etonedu.meals.http;

import android.os.Build;

import com.blankj.utilcode.util.LogUtils;

import com.etonedu.meals.BuildConfig;
import com.etonedu.meals.bean.CardLog;
import com.etonedu.meals.bean.Result;
import com.etonedu.meals.common.SPConst;
import com.etonedu.meals.dao.DaoUtils;
import com.etonedu.meals.enumeration.APICodeEnum;
import com.etonedu.meals.http.xutils.NewXUtilsRequest;
import com.etonedu.meals.http.xutils.NewXUtilsResponse;
import com.etonedu.meals.utils.SharedPreferencesUtil;
import com.etonedu.meals.utils.Utils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.xutils.http.HttpMethod;
import org.xutils.x;

import java.lang.reflect.Type;
import java.util.HashMap;

public class LogReportUtils {

    //1 开关机时间设置 2 手动关机 3 手动开屏 4 手动关屏 5	自动开机 6 自动关机 7 自动开屏 8	自动关屏 9 设置亮度 10	设置音量 11 硬件开机 12 切换模板 13 界面截屏 14 开关屏时间设置 15 考勤时间设置 16 绑定模板
    public static void logReportPublicUrl(final String logId, final String opType, final String isSuccess, final String content) {
        String apiCode = APICodeEnum.agw30161004.getApiCode();
        final SharedPreferencesUtil spu = SharedPreferencesUtil.getInstance(x.app());
        String type = spu.getString(SPConst.ENVIRONMENT);
        final String token = spu.getString(SPConst.TOKEN);
//        String environType = EnvironmentType.TEST01.getType();

        String url = spu.getString(SPConst.AGW_30161004);
        if (!Utils.isEmpty(url)) {
            logReport(token,url,logId,opType,isSuccess,content);
            return;
        }

        com.etonedu.meals.http.Request request = new com.etonedu.meals.http.Request();
        request.getPublicUrl(apiCode,type);
        request.setGetUrl(new IGetUrl() {
            @Override
            public void success(String url) {

                LogUtils.e("url = "+url);
                spu.setString(SPConst.AGW_30161004,url);
                logReport(token,url,logId,opType,isSuccess,content);
            }

            @Override
            public void fail(String content) {
                LogUtils.e("content = "+content);
            }
        });
    }

    private static void logReport(String token, String url, String logId, String opType, String isSuccess, String content) {
        final String apiCode = APICodeEnum.agw30161004.getApiCode();
        String header = ParamsUtils.getHeader(apiCode, token);

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("deviceCode", Utils.getDevCode());
        hashMap.put("logId", logId);
        //1 开关机时间设置 2 手动关机 3 手动开屏 4 手动关屏 5	自动开机 6 自动关机 7 自动开屏 8	自动关屏 9 设置亮度 10	设置音量 11 硬件开机 12 切换模板 13 界面截屏 14 开关屏时间设置 15 考勤时间设置 16 绑定模板
        hashMap.put("opType", opType);

        hashMap.put("isSuccess", isSuccess);//1 成功 2 失败
        hashMap.put("content", content);
        Gson gson = new Gson();
        String json = gson.toJson(hashMap);

        String data = ParamsUtils.getEncrypt(apiCode,json);

        String data1 = ParamsUtils.getParams(apiCode,data);

        NewXUtilsRequest request = new NewXUtilsRequest(header, data1);
        request.requet(url, apiCode, HttpMethod.POST, new NewXUtilsResponse(){

            @Override
            public void response(String type, String result) {
                LogUtils.e("result = "+result);

                Type typeToken = new TypeToken<Result>() {
                }.getType();
                Gson gson = new Gson();
                Result r = gson.fromJson(result, typeToken);

                String data = r.getData();

                String rescode = r.getRescode();

                if ("13008".equals(rescode)) {
                    SharedPreferencesUtil spu = SharedPreferencesUtil.getInstance(x.app());
                    spu.clear();
                }

                String deData = ParamsUtils.getDecrypt(apiCode,data);
//                LogUtils.e("deData = "+deData);
                LogUtils.json(deData);

            }

            @Override
            public void error(String type, String msg) {

            }
        });
    }

    public static void stateReportPublicUrl() {
        String apiCode = APICodeEnum.agw30161002.getApiCode();
        final SharedPreferencesUtil spu = SharedPreferencesUtil.getInstance(x.app());
        String type = spu.getString(SPConst.ENVIRONMENT);
        final String token = spu.getString(SPConst.TOKEN);
//        String environType = EnvironmentType.TEST01.getType();

        String url = spu.getString(SPConst.AGW_30161002);
        if (!Utils.isEmpty(url)) {
            stateReport(token,url);
            return;
        }

        com.etonedu.meals.http.Request request = new com.etonedu.meals.http.Request();
        request.getPublicUrl(apiCode,type);
        request.setGetUrl(new IGetUrl() {
            @Override
            public void success(String url) {

                LogUtils.e("url = "+url);
                spu.setString(SPConst.AGW_30161002,url);
                stateReport(token,url);
            }

            @Override
            public void fail(String content) {
                LogUtils.e("content = "+content);
            }
        });
    }

    private static void stateReport(String token, String url) {
        final String apiCode = APICodeEnum.agw30161002.getApiCode();
        String header = ParamsUtils.getHeader(apiCode, token);

        String model = Build.MODEL;
        int britness = Utils.getBritness(x.app());
        int volume = Utils.getStreamVolume(x.app());
        String appVersion = Utils.getVersion1(x.app());
        int verId = Utils.getVersionCode(x.app());

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("deviceCode", Utils.getDevCode());
        hashMap.put("modelNo", model);
        hashMap.put("luminance", britness);

        hashMap.put("volume", volume);
        hashMap.put("appVerId", verId);
        hashMap.put("appVersion", appVersion);
        Gson gson = new Gson();
        String json = gson.toJson(hashMap);

        String data = ParamsUtils.getEncrypt(apiCode,json);

        String data1 = ParamsUtils.getParams(apiCode,data);

        NewXUtilsRequest request = new NewXUtilsRequest(header, data1);
        request.requet(url, apiCode, HttpMethod.POST, new NewXUtilsResponse(){

            @Override
            public void response(String type, String result) {
                LogUtils.e("result = "+result);

                Type typeToken = new TypeToken<Result>() {
                }.getType();
                Gson gson = new Gson();
                Result r = gson.fromJson(result, typeToken);

                String data = r.getData();

                if (!Utils.isEmpty(data)) {
                    String deData = ParamsUtils.getDecrypt(apiCode, data);
                    LogUtils.json(deData);
                }
            }

            @Override
            public void error(String type, String msg) {

            }
        });
    }

    public static void getPublicStateUrl(final String state) {
        String apiCode = APICodeEnum.agw30161001.getApiCode();
        final SharedPreferencesUtil spu = SharedPreferencesUtil.getInstance(x.app());
        String type = spu.getString(SPConst.ENVIRONMENT);
        final String token = spu.getString(SPConst.TOKEN);
//        String environType = EnvironmentType.TEST01.getType();

        String url = spu.getString(SPConst.AGW_30161001);
        if (!Utils.isEmpty(url)) {
            state(token,url,state);
            return;
        }

        com.etonedu.meals.http.Request request = new com.etonedu.meals.http.Request();
        request.getPublicUrl(apiCode,type);
        request.setGetUrl(new IGetUrl() {
            @Override
            public void success(String url) {

                LogUtils.e("url = "+url);
                spu.setString(SPConst.AGW_30161001,url);
                state(token,url,state);
            }

            @Override
            public void fail(String content) {
                LogUtils.e("content = "+content);
            }
        });
    }

    private static void state(String token, String url, String state) {
        final String apiCode = APICodeEnum.agw30161001.getApiCode();
        String header = ParamsUtils.getHeader(apiCode, token);

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("deviceCode", Utils.getDevCode());
        hashMap.put("status", state);

        Gson gson = new Gson();
        String json = gson.toJson(hashMap);

        String data = ParamsUtils.getEncrypt(apiCode,json);

        String data1 = ParamsUtils.getParams(apiCode,data);

        NewXUtilsRequest request = new NewXUtilsRequest(header, data1);

        request.requet(url, apiCode, HttpMethod.POST, new NewXUtilsResponse(){

            @Override
            public void response(String type, String result) {
                LogUtils.e("result = "+result);

                Type typeToken = new TypeToken<Result>() {
                }.getType();
                Gson gson = new Gson();
                Result r = gson.fromJson(result, typeToken);

                String data = r.getData();

                String deData = ParamsUtils.getDecrypt(apiCode,data);
//                LogUtils.e("deData = "+deData);
                LogUtils.json(deData);

            }

            @Override
            public void error(String type, String msg) {

            }
        });
    }

    // 心跳
    public static void getPublicUrl(final String token) {
        String apiCode = APICodeEnum.agw30161011.getApiCode();
        final SharedPreferencesUtil spu = SharedPreferencesUtil.getInstance(x.app());
        String type = spu.getString(SPConst.ENVIRONMENT);
//        String environType = EnvironmentType.TEST01.getType();

        String url = spu.getString(SPConst.AGW_30161011);
        if (!Utils.isEmpty(url)) {
            getData(url,token);
            return;
        }

        com.etonedu.meals.http.Request request = new com.etonedu.meals.http.Request();
        request.getPublicUrl(apiCode,type);
        request.setGetUrl(new IGetUrl() {
            @Override
            public void success(String content) {
                spu.setString(SPConst.AGW_30161011,content);
                getData(content,token);
                LogUtils.e("content = "+content);
            }

            @Override
            public void fail(String content) {
                LogUtils.e("content = "+content);
            }
        });
    }

    private static void getData(String url , String token) {
        final String apiCode = APICodeEnum.agw30161011.getApiCode();
        String header = ParamsUtils.getHeader(apiCode, token);

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("deviceCode", Utils.getDevCode());
//        hashMap.put("status",1);
        Gson gson = new Gson();
        String json = gson.toJson(hashMap);

        String data = ParamsUtils.getEncrypt(apiCode,json);

        String data1 = ParamsUtils.getParams(apiCode,data);

        NewXUtilsRequest request = new NewXUtilsRequest(header, data1);
        request.requet(url, apiCode, HttpMethod.POST, new NewXUtilsResponse(){

            @Override
            public void response(String type, String result) {
                LogUtils.e("result = "+result);

                Type typeToken = new TypeToken<Result>() {
                }.getType();
                Gson gson = new Gson();
                Result r = gson.fromJson(result, typeToken);

                String data = r.getData();

                String deData = ParamsUtils.getDecrypt(apiCode,data);
                LogUtils.e("deData = "+deData);


            }

            @Override
            public void error(String type, String msg) {

            }
        });
    }

    public static void log(String logContent,String className,String lineNumber,String logTime) {

        if (BuildConfig.DEBUG) return;

        SharedPreferencesUtil spu = SharedPreferencesUtil.getInstance(x.app());
        String devId = Utils.getDevCode();
        String model = Build.MODEL;

        String schoolName = spu.getString(SPConst.SCHOOL_NAME);
        String gradeName = spu.getString(SPConst.CLASS_NAME);
        String schNameAndGradeName = schoolName;
        if (!Utils.isEmpty(gradeName)) {
            schNameAndGradeName = schoolName + ","+gradeName;
        }

        int vesionCode = Utils.getVersionCode(x.app());
        String vesionName = Utils.getVersion(x.app());

        CardLog cardLog = new CardLog();
        cardLog.setDevice_code(devId);
        cardLog.setSchool_name(schNameAndGradeName);
        cardLog.setLog_content(logContent);
        cardLog.setClass_name(className);
        cardLog.setLine_number(lineNumber);
        cardLog.setApp_version_code(vesionCode);
        cardLog.setApp_version_name(vesionName);
        cardLog.setLog_time(logTime);
        cardLog.setUid(className);

        DaoUtils.insertCardLog(cardLog);

    }

}
